package cn.xuqiudong.rpc.protocol.netty.handler;

import cn.xuqiudong.rpc.common.model.XqdRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.xuqiudong.rpc.common.util.ExecutorPoolUtils;
import cn.xuqiudong.rpc.protocol.netty.model.NettyProtocolData;

import java.util.concurrent.Executor;

/**
 * 描述:netty服务端的处理器
 * @author Vic.xu
 * @date 2022-02-18 11:19
 */
public class XqdNettyServerHandler extends SimpleChannelInboundHandler<NettyProtocolData<XqdRequest>> {

   private static Logger logger = LoggerFactory.getLogger(XqdNettyServerHandler.class);

    /**
     * 创建一个处理请求的线程池
     */
    Executor executor = ExecutorPoolUtils.createExecutor(20, this.getClass().getSimpleName());

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        SocketChannel channel = (SocketChannel) ctx.channel();
        logger.info("有一客户端链接到本服务端，ip={},port=[4{}",channel.localAddress().getHostString(), channel.localAddress().getPort());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, NettyProtocolData<XqdRequest> msg) throws Exception {
        logger.debug("开始处理服务端的请求");
        executor.execute(new XqdChannelRunnable(ctx, msg));
    }

}
